# coding=utf-8
# Date: 2021/9/27 10:06


# 执行用时：7024 ms, 在所有 Python3 提交中击败了5.40%的用户
# 内存消耗：19 MB, 在所有 Python3 提交中击败了63.51%的用户
class Solution:
    def numDecodings(self, s: str) -> int:
        n, MOD = len(s), int(1e9) + 7
        f = [0] * (n + 1)
        f[0] = 1
        for i in range(1, n + 1):
            for j in range(1, 27):
                a = s[i - 1]
                if j <= 9:
                    if a == '*' or a == chr(j + ord('0')):
                        f[i] = (f[i] + f[i - 1]) % MOD
                elif i >= 2:
                    b = s[i - 2]
                    y, x = j // 10, j % 10
                    if (b == chr(y + ord('0')) or b == '*' and y != 0) and \
                            (a == chr(x + ord('0')) or a == '*' and x != 0):
                        f[i] = (f[i] + f[i - 2]) % MOD
        return f[n]


if __name__ == "__main__":
    print(Solution().numDecodings("*"))  # 9
    print(Solution().numDecodings("1*"))  # 18
    print(Solution().numDecodings("**"))  # 96
